#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QBitmap>
#include <QDebug>
#include <QPushButton>
#include <QMouseEvent>
#include "config.h"
#include <iostream>
using namespace std;
Widget::Widget(QWidget *parent) : QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化
    init();
    //初始化事件绑定
   initEvent();


}
/**
 *初始化
 * @brief Widget::init
 */
void Widget:: init(){
    //设置标题
    this->setWindowTitle("控件");
    //设置最大宽高
    this->setMaximumSize(MAX_WIDTH, MAX_HEIGH);
    //设置最小宽高
    this->setMinimumSize(MIN_WIDTH, MIN_HEIGH);
    //设置位置和大小
    this->setGeometry(POS_X, POS_Y, MAX_WIDTH, MAX_HEIGH);
    //设置位置
    this->move(300, 300);
    //设置背景色
    //this->setStyleSheet("background:#FFFFFF");
    //设置背景图片
//    QPixmap pixmap = QPixmap("resources/timg.jpeg").scaled(this->size());
//    QPalette  palette (this->palette()); //调色板
//    palette.setBrush(QPalette::Background, QBrush(pixmap));
//    this-> setPalette( palette );

    //设置图标
    this->icon = new QIcon(QBitmap("resources/timg.jpeg"));
    this->setWindowIcon(*icon);

    //设置无边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    //设置透明背景
    //this->setAttribute(Qt::WA_TranslucentBackground, true);



    //关闭按钮
    closeBtn =  new QPushButton(this);
    closeBtn->setStyleSheet("background:#F44336");
    closeBtn->setGeometry(width() - closeBtn->width(), 10, 80, 30);
    closeBtn->setText("关闭窗口");

    //打开窗口按钮
    newWinBtn = new QPushButton(this);
    newWinBtn->setStyleSheet("background:#FFEB38");
    newWinBtn->setGeometry(10, 10, 80, 30);
    newWinBtn->setText("打开新窗口");
    //关闭子窗口按钮
    closeWinBtn = new QPushButton(this);
    closeWinBtn->setStyleSheet("background:#FFEB38");
    closeWinBtn->setGeometry(300, 10, 80, 30);
    closeWinBtn->setText("关闭新窗口");

    //label
    label = new QLabel("我是QLabel", this);
    //位置
   // label->move(10, 100);
    label->setGeometry(10, 100, 200, 30);
    //红色斜体加粗
    label->setStyleSheet("font-size:20px;color:red;font-weight:bold;font-style:italic");

    //单行文本
    lineEdit = new QLineEdit(this);
    lineEdit->setText("单行文本框");
    //位置大小
    lineEdit->setGeometry(250, 100, 150, 30);
    //border 边框大小
    //border-style 边框样式 solid 实线
    //border-color:blue red 上下蓝色 左右红色
    lineEdit->setStyleSheet("border:1px;border-style:solid;color:red;border-color:blue red");

    //多行文本
    textEdit = new QTextEdit(this);
    //位置大小
    textEdit->setGeometry(QRect(10, 150, 200, 100));
    textEdit->setText("第一行多行文本框<br/>第二行");

    //纯多行文本编辑器
    plainTextEdit = new QPlainTextEdit(this);
    plainTextEdit->setGeometry(QRect(250, 150, 200, 100));
    plainTextEdit->setPlainText("第一行\n第二行");

    //下拉列表框
    comboBox = new QComboBox(this);
    comboBox->setGeometry(500, 150, 100, 30);
    //字符串列表
    QStringList strList;
    strList << "语文" << "数学" << "英语";
    comboBox->addItems(strList);

    //字体下拉框
    fontComBox = new QFontComboBox(this);
    fontComBox->setGeometry(620, 150, 100, 30);

    //范围选择
    spinBox = new QSpinBox(this);
    spinBox->setGeometry(10, 280, 100, 30);
    spinBox->setRange(0, 200);//设置范围
    spinBox->setValue(10); //设置值
    spinBox->setSuffix("美元");//后缀
    spinBox->setPrefix("$"); //前缀

    //时间控件
    timeEdit = new QTimeEdit(this);
    timeEdit->setGeometry(150, 280, 200, 30);
    //获取系统时间
     QDateTime sysTime = QDateTime::currentDateTime();
    //QDate date = sysTime.date();
    //timeEdit->setTime(sysTime.time());
    // timeEdit->setDateTime(sysTime);
    QStringList list = sysTime.toString("hh:mm:ss").split(":");
    timeEdit->setTime(QTime(list[0].toInt(), list[1].toInt(), list[2].toInt()));


    //日期控件
    dateEdit = new QDateEdit(this);
    dateEdit->setGeometry(400, 280, 200, 30);
    //dateEdit->setDate(sysTime.date());
    QStringList dateList = sysTime.toString("yyyy-MM-dd").split("-");
    dateEdit->setDate(QDate(dateList[0].toInt(), dateList[1].toInt(), dateList[2].toInt()));

    //滚动条
    scrollBar = new QScrollBar(this);
    //设置方向
    scrollBar->setOrientation(Qt::Orientation::Horizontal);
    //位置
    scrollBar->setGeometry(10, 320, 180, 20);
    scrollBar->setMaximum(200);
    scrollBar->setMinimum(0);
    scrollBar->setValue(0);

    //单选按钮
    radioBtn1 = new QRadioButton(this);
    radioBtn2 = new QRadioButton(this);
    radioBtn1->setGeometry(200, 320, 50, 50);
    radioBtn2->setGeometry(280, 320, 50, 50);
    radioBtn1->setText("男");
    radioBtn2->setText("女");
    radioBtn1->setChecked(true);

    //复选框
    //实例化 checkBox 控件
    checkBox01 = new QCheckBox(this);
    checkBox02 = new QCheckBox(this);
    checkBox03 = new QCheckBox(this);
    //控件位置
    checkBox01->setGeometry(QRect(400, 320, 50, 50));
    checkBox02->setGeometry(QRect(480, 320, 50, 50));
    checkBox03->setGeometry(QRect(560, 320, 50, 50));
    //控件值
    checkBox01->setText("数学");
    checkBox02->setText("语文");
    checkBox03->setText("地理");

    //listView
    //实例 listView 控件
    listView = new QListView(this);
    //定义位置宽高
    listView->setGeometry(QRect(10,360,100,80));
    //StringList 数组
    QStringList string;
    string << "数学" << "语文" << "外语" <<"地理";
    //添加数据
    model = new QStringListModel(string);
    //将数据绑定 listView 控件
    listView-> setModel(model);

    //树形结构试图
    //实例 QTreeView 控件
    treeView = new QTreeView(this);
    //位置
    treeView->setGeometry(QRect(160,360,150,120));
    //实例数据类型 4 个节点,2 列
    itemModel = new QStandardItemModel(3,2);
    //列名称
    itemModel->setHeaderData(0,Qt::Horizontal,"第一列");
    itemModel->setHeaderData(1,Qt::Horizontal,"第二列");
    //定义节点
    QStandardItem *item1 = new QStandardItem("数学");
    item1->setIcon(QIcon("resources/folder.png"));

    QStandardItem *item2 = new QStandardItem("语文");
    item2->setIcon(QIcon("resources/folder.png"));

    QStandardItem *item3 = new QStandardItem("外语");
    item3->setIcon(QIcon("resources/folder.png"));
    //外语子项
    QStandardItem *item4 = new QStandardItem("外语 A");
    item4->setIcon(QIcon(":/new/prefix1/file"));
    item3->appendRow(item4);

    //将节点添加至 QStandardItemModel
    itemModel->setItem(0,0,item1); //第0行第0列
    itemModel->setItem(1,0,item2);
    itemModel->setItem(2,0,item3);
    //将 QStandardItemModel 数据绑定 QTreeView 控件
    treeView->setModel(itemModel);


    //表格视图
    //实例
    tableView = new QTableView(this);
    //位置
    tableView->setGeometry(QRect(320,360,320,100));
    //实例数据模型
    tableModel = new QStandardItemModel();
    //定义列
    tableModel->setHorizontalHeaderItem(0,new QStandardItem("数学"));
    tableModel->setHorizontalHeaderItem(1,new QStandardItem("语文"));
    tableModel->setHorizontalHeaderItem(2,new QStandardItem("外语"));
    tableModel->setItem(0,0,new QStandardItem("90"));
    tableModel->setItem(0,1,new QStandardItem("90"));
    tableModel->setItem(0,2,new QStandardItem("99"));
    tableModel->setItem(1,0,new QStandardItem("80"));
    tableModel->setItem(1,1,new QStandardItem("80"));
    tableModel->setItem(1,2,new QStandardItem("85"));

    //将数据模型绑定控件
    tableView->setModel(tableModel);
}

/**
 *初始化事件绑定
 * @brief Widget::initEvent
 */
void Widget::initEvent(){
    //绑定事件和槽函数
    QObject::connect(closeBtn, SIGNAL(clicked()), this, SLOT(close()));
    QObject::connect(newWinBtn, SIGNAL(clicked()), this, SLOT(showNewWidget()));
    QObject::connect(closeWinBtn, SIGNAL(clicked()), this, SLOT(closeNewWidget()));
    QObject::connect(fontComBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(getFont(QFont)));
    QObject::connect(scrollBar, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
    QObject::connect(radioBtn1, SIGNAL(clicked()), this, SLOT(radioChanged()));
    QObject::connect(radioBtn2, SIGNAL(clicked()), this, SLOT(radioChanged()));
    //checkbox 控件点击事件
    QObject::connect(checkBox01, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
    QObject::connect(checkBox02, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
    QObject::connect(checkBox03, SIGNAL(clicked(bool)), this, SLOT(checkChange()));

}
/**
 * 显示一个新窗口
 * @brief Widget::showNewWidget
 */
void Widget::showNewWidget(){
   if(newWidget == nullptr){
       qDebug() << "创建了newWidget"<<"\n";
       newWidget = new  QWidget(this);
       //newWidget = new  NewDialog(this);
       newWidget->setGeometry(POS_X + 50, POS_Y + 50, MIN_WIDTH, MIN_HEIGH);
       newWidget->setStyleSheet("background:#009688");
   }
   qDebug() << "显示新窗口" <<"\n";
   newWidget->show();
}
/**
 *关闭新窗口
 * @brief Widget::closeNewWidget
 */
void Widget::closeNewWidget(){
    if(newWidget !=  nullptr){
        newWidget->close();
    }
}


//获取选择的字体
void Widget::getFont(const QFont& f){
    label->setFont(f);
}
//单选按钮发生改变
void Widget::radioChanged(){
    if(sender() == radioBtn1){
        label->setText("男");
    }else if(sender() == radioBtn2){
        label->setText("女");
    }
}
//复选框改变
void Widget::checkChange(){
    QCheckBox * checkBox = dynamic_cast<QCheckBox *>(sender());
    //判断是否被选中
    if(checkBox->checkState() == Qt::Checked){
        str += checkBox->text();
    }else{
        str = str.replace(QString(checkBox->text()),QString(""));
    }
    label->setText(str);
}
//鼠标按下
void Widget::mousePressEvent(QMouseEvent* e){
    last = e->globalPos(); //设置鼠标位置
}
//鼠标移动（实现窗口跟随鼠标移动效果）
void Widget::mouseMoveEvent(QMouseEvent* e){
   // qDebug() << e->x() << "," << e->globalX() << "\n";
   //globalX() 全局（屏幕）坐标
    int dx, dy;
    dx = e->globalX() - last.x(); //x方向偏移量
    dy = e->globalY() - last.y(); //y方向偏移量
    last = e->globalPos();

    if(newWidget != nullptr
            && e->x() >= newWidget->x()
            && e->x() <= newWidget->x()+newWidget->width()
            && e->y() >= newWidget->y()
            && e->y() <= newWidget->y() + newWidget->height()){
        newWidget->move(newWidget->x() + dx, newWidget->y() + dy);
    }else{
        move(x() + dx, y() + dy); //窗口移动
    }
}
//鼠标释放
void Widget::mouseReleaseEvent(QMouseEvent* ){
//    int dx = e->globalX() - last.x(); //x方向偏移量
//    int dy = e->globalY() - last.y(); //y方向偏移量
//    last = e->globalPos();
//    move(x() + dx, y() + dy); //窗口移动
}
Widget::~Widget()
{/*
    delete icon;
    delete closeBtn;
    delete newWinBtn;
    delete newWidget;
    delete closeWinBtn;*/
    delete ui;
}
